#include<bits/stdc++.h>
using namespace std;
#define int long long
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return x*f;
}
const int N=405;
int n,p[N],a[N],b[N],ans;
vector<int> g[N];
int work(){
    int ans=0;
	for(int i=1;i<=n;i++){
		ans+=a[p[i]];
        for(auto x:g[p[i]]) a[x]+=a[p[i]];
	}
    return ans;
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
    n=read();
	for(int i=1;i<=n;i++) a[i]=b[i]=read(),p[i]=i;
	for(int i=1;i<n;i++){
        int x=read(),y=read();
		g[x].push_back(y),g[y].push_back(x);
	}
	do{
		memcpy(a,b,sizeof(b));
		ans=max(ans,work());
	}while(next_permutation(p+1,p+1+n));
    cout<<ans;
	return 0;
}
